Mappa
viridis_option = "C"
affluenza_discrete = seq(floor(100*min(hexagon$affluenza)),
ceiling(100*max(hexagon$affluenza)),
length = 8) %>% floor()/100
pal = colorNumeric(viridis_pal(option = viridis_option, direction = -1)(20),
range(affluenza_discrete))
legend_color = viridis_pal(option = viridis_option, direction = 1)(length(affluenza_discrete)-1)
legend_labels = paste0(label_percent()(affluenza_discrete)[-length(affluenza_discrete)], " - ",
label_percent()(affluenza_discrete)[-1]) %>% rev()
legend_labels[1] = legend_labels[1] %>% gsub("-.*", "- Max", .)
legend_labels[length(legend_labels)] = legend_labels[length(legend_labels)] %>% gsub(".*-", "Min -", .)
centroids = centroids %>%
mutate(affluenza_col = affluenza %>% ifelse(. <= min(affluenza_discrete), min(affluenza_discrete), .) %>%
ifelse(. >= max(affluenza_discrete), max(affluenza_discrete), .))
contour = contour %>%
mutate(affluenza_col = Max %>% ifelse(. <= min(affluenza_discrete), min(affluenza_discrete), .) %>%
ifelse(. >= max(affluenza_discrete), max(affluenza_discrete), .))
sezioni = sezioni %>%
mutate(affluenza_col = affluenza %>% ifelse(. <= min(affluenza_discrete), min(affluenza_discrete), .) %>%
ifelse(. >= max(affluenza_discrete), max(affluenza_discrete), .))
leaflet() %>%
addProviderTiles("CartoDB.Positron",
options = providerTileOptions(minZoom = 12, maxZoom = 16, className = "dark-map-tiles")) %>%
setView(lng = 11.3493292, lat = 44.492245, zoom = 12) %>%
setMaxBounds(lng1 = 11.22814, lat1 = 44.41880, lng2 = 11.43364, lat2 = 44.55736) %>%
addPolygons(data = hexagon, group = "Hexagon density",
color = ~pal(affluenza), opacity = 0.5,
fillColor = ~pal(affluenza), fillOpacity = 0.7,
weight = 0.5, label = ~affluenza_label) %>%
addCircleMarkers(data = centroids, group = "Centroids sezioni",
color = ~pal(affluenza_col), radius = 8,
stroke = FALSE, fillOpacity = 0.5) %>% hideGroup("Centroids sezioni") %>%
addPolygons(data = contour, group = "Contour lines",
weight = 0.5, opacity = 1, fillOpacity = 0.5,
color = ~pal(affluenza_col),
smoothFactor = 0.1, label = ~var1.pred,
highlight = highlightOptions(weight = 1, fillOpacity = 1, color = "white",
bringToFront = TRUE,sendToBack = TRUE)) %>%
addPolygons(data = sezioni, group = "Sezioni density",
weight = 0.5, opacity = 1, fillOpacity = 0.5,
color = ~pal(affluenza_col),
smoothFactor = 0.1, label = ~affluenza_label,
highlight = highlightOptions(weight = 1, fillOpacity = 1, color = "white",
bringToFront = TRUE,sendToBack = TRUE)) %>%
addLayersControl(
options = layersControlOptions(collapsed = FALSE),
overlayGroups = c("Centroids sezioni"),
baseGroups = c("Hexagon density", "Contour lines", "Sezioni density")
) %>%
addLegend("bottomright",
colors = legend_color,
labels = legend_labels,
opacity = 0.7, title = "Affluenza %") %>%
addResetMapButton()